﻿using System;
using System.Windows.Forms;

namespace LM.Tela.Desktop.Formularios
{
    using LM.InterfacesFabricas.Excecoes;
    using System.Linq;
    using System.Threading;

    public partial class frmPrincipal : Form
    {
        public frmPrincipal()
        {
            InitializeComponent();
            Logar();
        }

        static frmPrincipal()
        {
            AppDomain.CurrentDomain.UnhandledException += TrateExcecoesNaoTratadas;
            Application.ThreadException += ApplicationOnThreadException;
        }

        private static void ApplicationOnThreadException( object sender, ThreadExceptionEventArgs threadExceptionEventArgs )
        {
            TrateExcecao( threadExceptionEventArgs.Exception );
        }

        private static void TrateExcecoesNaoTratadas( object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs )
        {
            TrateExcecao( unhandledExceptionEventArgs.ExceptionObject as Exception );
        }

        private static void TrateExcecao( Exception ex )
        {
            var inconsistencia = ex as InconsistenciasException;

            if ( inconsistencia != null )
            {
                MessageBox.Show(
                    string.Format(
                        "Inconsistências encontradas:\r\n\r\n{0}",
                        string.Join( "\r\n", inconsistencia.Inconsistencias ) ),
                    "Inconsistência! - Não foi possível realizar a operação.",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation );

            }
            else
            {
                MessageBox.Show(
                   ex.ToString(),
                   "Erro!",
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Error );
            }
        }

        private void CrieNovoFormulario<T>() where T : Form
        {
            var frm = MdiChildren.FirstOrDefault( f => f.Name == typeof( T ).Name );

            if ( frm == null )
            {
                var novoForm = Activator.CreateInstance( typeof( T ) ) as Form;
                novoForm.MdiParent = this;
                novoForm.Show();
            }
            else
            {
                frm.Activate();
            }
        }

        private void mMarca_Click( object sender, EventArgs e )
        {
            CrieNovoFormulario<frmMarca>();
        }

        private void sairToolStripMenuItem_Click( object sender, EventArgs e )
        {
            Close();
        }

        private void Logar()
        {
            frmLogin login = new frmLogin();

            login.ShowDialog();

            if ( login.DialogResult != System.Windows.Forms.DialogResult.OK )
            {
                DialogResult = System.Windows.Forms.DialogResult.Abort;
                Close();
                return;
            }

            stOperadorLogado.Text = login.OperadorLogado;
        }

        private void mCadTabelaPreco_Click( object sender, EventArgs e )
        {
            CrieNovoFormulario<frmTabelaPreco>();
        }

        private void garagemToolStripMenuItem_Click( object sender, EventArgs e )
        {
            CrieNovoFormulario<frmGaragem>();
        }

        private void toolStripMenuItem3_Click( object sender, EventArgs e )
        {
            CrieNovoFormulario<frmVaga>();
        }

        private void operadorToolStripMenuItem_Click( object sender, EventArgs e )
        {
            CrieNovoFormulario<frmCadastroOperador>();
        }

        private void mCadCliente_Click( object sender, EventArgs e )
        {
            CrieNovoFormulario<frmCadastroDeCliente>();
        }

        private void mTipoVeiculo_Click( object sender, EventArgs e )
        {
            CrieNovoFormulario<frmCadastroDeTipoDeVeiculo>();
        }

        private void mLocLocacao_Click( object sender, EventArgs e )
        {
            CrieNovoFormulario<frmLocacao>();
        }

        private void mModelo_Click( object sender, EventArgs e )
        {
            CrieNovoFormulario<frmCadastroDeModelo>();
        }

        private void mSobre_Click( object sender, EventArgs e )
        {
            //new frmSobre().ShowDialog();
        }

        private void frmPrincipal_FormClosing( object sender, FormClosingEventArgs e )
        {
            e.Cancel = ( MessageBox.Show( "Deseja realmente sair?", "Atenção", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) != System.Windows.Forms.DialogResult.Yes );
        }

    }
}
